//
//  WWebViewToolController.swift
//  iiatn
//
//  Created by mine on 18/4/8.
//  Copyright © 2018年 mnine. All rights reserved.
//

import UIKit
import WebKit
import SVProgressHUD

class WWebViewToolController: itBaseViewController,WKNavigationDelegate,WKUIDelegate , GetCancelMessageDelegate{
    
    private var webView = WKWebView()
    private var progressView = UIProgressView()
    var open_url:String?
    
    
    override func viewDidLoad() {
        
        super.viewDidLoad()
        

        SVProgressHUD.showProgress(0.8)
        // Do any additional setup after loading the view.
        webView = WKWebView(frame: CGRect(x: 0, y: 0, width: SCREENW, height: SCREENH))
        webView.navigationDelegate = self
        webView.uiDelegate = self;
        let url = URL(string: open_url!)
        print(url ?? 0)
        let request = URLRequest(url: url!)
        webView.load(request)
        /**
         增加的属性：
         1.webView.estimatedProgress加载进度
         2.backForwardList 表示historyList
         3.WKWebViewConfiguration *configuration; 初始化webview的配置
         */
        self.view.addSubview(webView)
        webView.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
        progressView = UIProgressView(frame: CGRect(x: 0, y: 44-2, width: UIScreen.main.bounds.size.width, height: 4))
        progressView.trackTintColor = UIColor.white
        progressView.progressTintColor = UIColor.orange
        self.navigationController?.navigationBar.addSubview(progressView)
        
        webView.snp.makeConstraints { (make) in
            make.edges.equalTo(view)
        }
    }
    
    override func setupNav() {
        view.backgroundColor = UIColor.white
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "checkUserType_backward_9x15_"), style: .plain, target: self, action: #selector(backItemPressed))
    }
    
    func backItemPressed() {
        if webView.canGoBack {
            webView.goBack()
        }else{
            if let nav = self.navigationController {
                nav.popViewController(animated: true)
            }
        }
    }
    override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if (keyPath == "estimatedProgress") {
            progressView.isHidden = webView.estimatedProgress == 1
            progressView.setProgress(Float(webView.estimatedProgress), animated: true)
            print(webView.estimatedProgress)
        }
    }
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        progressView.setProgress(0.0, animated: false)
        SVProgressHUD.dismiss()
        if webView.title! == "国别项目全览" {
            if !whaslogin! {
                let alertController = UIAlertController(title: "会员专享",
                                                        message: "\n您还没有登录，是否前往登录？", preferredStyle: .alert)
                let cancelAction = UIAlertAction(title: "返回", style: .cancel, handler: {
                    action in
                    self.backItemPressed()
                })
                let okAction = UIAlertAction(title: "确认", style: .default, handler: {
                    action in
                    self.toLogin()
                    
                })
                alertController.addAction(cancelAction)
                alertController.addAction(okAction)
                self.present(alertController, animated: true, completion: nil)
            }
        }
        // self.webView.scrollView.zoomScale = 0.2
        self.navigationItem.title = webView.title
    }
    func toLogin(){
        let loginVC = WLoginViewController()
        loginVC.title = "登录"
        loginVC.tag = true
        loginVC.delegate = self
        let nav = WNavigationController(rootViewController: loginVC)
        present(nav, animated: true, completion: nil)
    }
    func getCancelMessage(){
        backItemPressed()
    }
    
    
    deinit {
        print("con is deinit")
        SVProgressHUD.dismiss()
        webView.removeObserver(self, forKeyPath: "estimatedProgress")
        progressView.reloadInputViews()
    }
}
